﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BrightIdeasSoftware;

namespace Streambolics.Gui.Gpl
{
    /// <summary>
    ///     A decoration that displays an icon, a title (or name) and a description.
    /// </summary>
    /// <remarks><para>
    ///     This code is based on the VistaSelection example code by Phillip Piper
    /// </para></remarks>

    public class NamedDescriptionDecoration : AbstractDecoration
    {
        public ImageList ImageList;
        public string ImageName;
        public string Title;
        public string Description;

        public Font TitleFont = new Font ("Segoe UI", 9, FontStyle.Bold);
        public Color TitleColor = Color.FromArgb (255, 32, 32, 32);
        public Font DescripionFont = new Font ("Segoe UI", 9);
        public Color DescriptionColor = Color.FromArgb (255, 96, 96, 96);
        public Size CellPadding = new Size (2, 2);

        public override void Draw (ObjectListView olv, Graphics g, Rectangle r)
        {
            Rectangle cellBounds = CellBounds;
            cellBounds.Inflate (-CellPadding.Width, -CellPadding.Height);

            cellBounds.X += 20;
            cellBounds.Width -= 20;

            Rectangle textBounds = cellBounds;

            if (ImageList != null && !String.IsNullOrEmpty (ImageName))
            {
                g.DrawImage (ImageList.Images[ImageName], cellBounds.Location);
                textBounds.X += ImageList.ImageSize.Width + 4;
                textBounds.Width -= ImageList.ImageSize.Width + 4;
            }

            //g.DrawRectangle(Pens.Red, textBounds);

            // Draw the title
            using (StringFormat fmt = new StringFormat (StringFormatFlags.NoWrap))
            {
                fmt.Trimming = StringTrimming.EllipsisCharacter;
                fmt.Alignment = StringAlignment.Near;
                fmt.LineAlignment = StringAlignment.Near;
                using (SolidBrush b = new SolidBrush (this.TitleColor))
                {
                    g.DrawString (this.Title, this.TitleFont, b, textBounds, fmt);
                }
                // Draw the description
                SizeF size = g.MeasureString (this.Title, this.TitleFont, (int)textBounds.Width, fmt);
                textBounds.Y += (int)size.Height;
                textBounds.Height -= (int)size.Height;
            }

            // Draw the description
            using (StringFormat fmt2 = new StringFormat ())
            {
                fmt2.Trimming = StringTrimming.EllipsisCharacter;
                using (SolidBrush b = new SolidBrush (this.DescriptionColor))
                {
                    g.DrawString (this.Description, this.DescripionFont, b, textBounds, fmt2);
                }
            }
        }
    }
}
